<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>

<div id="clock"></div>
<button id="start" type="button">启动能源</button>
<button id="stop" type="button">关闭能源</button>

<script>
    /**
     * 4、使用定时器函数和Date对象实现，数字时钟（当前时间是：2019-06-06 18:40:52）
     */

    window.onload = function () {
        var start = document.getElementById("start");
        var stop = document.getElementById("stop");
        var times = null;
        start.onclick = function () {
            times = window.setInterval(localTime, 1000);
        }
        stop.onclick = function () {
            window.clearInterval(times);
        }

    }


    function localTime() {
        var myclock = document.getElementById("clock");
        var d = new Date();
        var yyyy = d.getFullYear();
        var months = d.getMonth() + 1;
        var MM;
        if (months < 10) {
            MM = "0" + months;
        } else {
            MM = months;
        }
        var days = d.getDate();
        var dd;
        if (days < 10) {
            dd = "0" + days;
        } else {
            dd = days;
        }
        var hours = d.getHours();
        var hh;
        if (hours < 10) {
            hh = "0" + hours;
        } else {
            hh = hours;
        }
        var minutes = d.getMinutes();
        var mm;
        if (minutes < 10) {
            mm = "0" + minutes;
        } else {
            mm = minutes;
        }
        var seconds = d.getSeconds();
        var ss;
        if (seconds < 10) {
            ss = "0" + seconds;
        } else {
            ss = seconds;
        }
        var str = yyyy + "-" + MM + "-" + dd + "-" + " " + hh + ":" + mm + ":" + ss;
        myclock.innerText = str;
        //times = window.setInterval(localTime, 1000);
    }

    //localTime();


</script>
</body>
</html>